//
//  SecViewController.m
//  10_Work 省市区
//
//  Created by Apple on 16/7/26.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "SecViewController.h"
#import "Province.h"
#import "City.h"
#import "District.h"
#define kTableView @"reuse"
#import "SecDistrictViewController.h"


@interface SecViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *bigArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createTableView];
    
}

- (void)createData {
    NSString *path = [NSString stringWithFormat:@"/Users/dllo/Desktop/10_Work 省市区/10_Work 省市区/area.txt"];
    NSString *dataStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *dataArray = [dataStr componentsSeparatedByString:@"\n"];
    self.bigArray = [NSMutableArray array];
    for (NSString *str in dataArray) {
        if (![str hasPrefix:@" "]) {
            Province *pro = [[Province alloc] initWithName:str];
            [self.bigArray addObject:pro];
        } else if (![str hasPrefix:@"   "]) {
            City *city = [[City alloc] initWithName:str];
            NSMutableArray *arr = [[self.bigArray lastObject] arr];
            [arr addObject:city];
        } else {
            District *dis = [[District alloc] initWithName:str];
            NSMutableArray *arr = [[[[self.bigArray lastObject] arr] lastObject] arr];
            [arr addObject:dis];
        }
    }
    NSLog(@"%@", self.bigArray);
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.bigArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.bigArray[section] name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.bigArray[section] arr].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
    cell.textLabel.text = [[self.bigArray[indexPath.section] arr][indexPath.row] name];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecDistrictViewController *vc = [[SecDistrictViewController alloc] init];
    vc.disArr = [[self.bigArray[indexPath.section] arr][indexPath.row] arr];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
